package com.jlcindia.util;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import com.jlcindia.hibernate.Customer;

public class HibernateTemplate {
	
	public static Object save(Object obj){
		Object id = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			id = session.save(obj);
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return id;
	}
	
	@SuppressWarnings("rawtypes")
	public static Object load(Class cls,Serializable id) {
		Object obj = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			obj =session.load(cls, id);
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
		return obj;
	}
	
	public static void update(Object obj){
		Transaction tx = null;
		try {
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			session.update(obj);
			tx.commit();
			session.close();
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("rawtypes")
	public static void delete(Class cls,Serializable id) {
		Object obj = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			obj =session.load(cls, id);
			session.delete(obj);
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
 			e.printStackTrace();
		}
	}
	
	public static List<Customer> findList(String hql){
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Query q = session.createQuery(hql);
		
			list = q.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
 			e.printStackTrace();
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public static List<Customer> findList(String hql,String arg){
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Query q = session.createQuery(hql);
			q = q.setParameter(0, arg);
			
			list = q.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
 			e.printStackTrace();
		}
		return list;
	}
	
	public static List<Customer> findList(String hql,double arg){
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Query q = session.createQuery(hql);
			q = q.setParameter(0, arg);
			
			list = q.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
 			e.printStackTrace();
		}
		return list;
	}
	
	public static List findList(String hql,int start,int total){
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Query q = session.createQuery(hql);
			q.setFirstResult(start);
			q.setMaxResults(total);
			
			list = q.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
 			e.printStackTrace();
		}
		return list;
	}
	
	public static List findList(String hql,int start,int total,String arg){
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Query q = session.createQuery(hql);
			q.setFirstResult(start);
			q.setMaxResults(total);
			
				q = q.setParameter(0, arg);
			
			list = q.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
 			e.printStackTrace();
		}
		return list;
	}
	
	public static List findList(String hql,int start,int total,double arg){
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Query q = session.createQuery(hql);
			q.setFirstResult(start);
			q.setMaxResults(total);
			
			q = q.setParameter(0, arg);
			
			list = q.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
 			e.printStackTrace();
		}
		return list;
	}
	
	public static List findList(String hql,String arg,String arg2){
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Query q = session.createQuery(hql);
			q = q.setParameter(0, arg);
			q = q.setParameter(1, arg2);
			list = q.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
 			e.printStackTrace();
		}
		return list;
	}
	
	public static List findList(String hql,String arg,String arg2,String arg3){
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Query q = session.createQuery(hql);
			q = q.setParameter(0, arg);
			q = q.setParameter(1, arg2);
			q = q.setParameter(2, arg3);
			list = q.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
 			e.printStackTrace();
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public static List findList(String hql,int start,int total,String arg,String arg2){
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Query q = session.createQuery(hql);
			q.setFirstResult(start);
			q.setMaxResults(total);
			q = q.setParameter(0, arg);
			q = q.setParameter(1, arg2);
			list = q.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
 			e.printStackTrace();
		}
		return list;
	}
	
	public static List findList(String hql,int start,int total,String arg,String arg2,String arg3){
		List<Customer> list = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Query q = session.createQuery(hql);
			q.setFirstResult(start);
			q.setMaxResults(total);
			q = q.setParameter(0, arg);
			q = q.setParameter(1, arg2);
			q = q.setParameter(2, arg3);
			list = q.list();
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
 			e.printStackTrace();
		}
		return list;
	}

	public static Object findObject(String hql,Object... args){
		Object obj = null;
		Transaction tx = null;
		try{
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();
			Query q = session.createQuery(hql);
			for(int i=0; i<args.length; i++){
				q = q.setParameter(i, args[i]);
			}
			obj = q.list().get(0);
			tx.commit();
			session.close();
		}
		catch(Exception e){
			if(tx!=null){
				tx.rollback();
			}
 			e.printStackTrace();
		}
		return obj;
	}

}
